package com.wison.quartz.job;

import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.http.Http;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/3.
 */
@IocBean
public class UserJob implements Job {

    @Inject
    private Dao dao;

    @Inject("java:$conf.get('server_url')")
    private String wsdlurl;


    @Inject("java:$conf.get('dwID')")
    private String dwID;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        try {
            Map<String, Object> parms = new HashMap<String, Object>();
           // Sql sql = Sqls.queryRecord("select user_lname xm ,user_no bh,user_card mac ,user_id sfz,user_telephone phone,user_serial dz,'1' bzint,'1' bz,user_dep bmid, sfid ,syje,'" + dwID + "' dw  from view_nt_zhye ");

            Sql sql = Sqls.queryRecord("SELECT TOP 100 user_lname xm ,user_no bh,user_card mac ,user_id sfz,user_telephone phone,user_serial dz,'1' bzint,'1' bz,user_dep bmid, sfid ,syje,'" + dwID + "'dw FROM         (        SELECT ROW_NUMBER() OVER (ORDER BY user_serial) AS RowNumber,* FROM view_nt_zhye        ) A WHERE RowNumber > 100*@page");

            int i = 0;

            while (true) {

                sql.setParam("page",i);
                //sql.setPager(new Pager(i, 100));
                dao.execute(sql);
                List<Record> list = sql.getList(Record.class);
                if (null == list || list.size() == 0) {
                    break;
                }
                parms.put("cards", Json.toJson(list));
                String response = Http.post(wsdlurl + "/saveUser.do", parms, 50 * 1000);
                System.out.println(response);
                i++;
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}
